<script>
    $(document).ready(function() {
        $("#datepickerstart").datepicker({
            numberOfMonths: 1,
            onSelect: function(selected) {
                $("#datepickerend").datepicker("option", "minDate", selected)
            }
        });
        $("#datepickerend").datepicker({
            numberOfMonths: 1,
        });
    });

</script>
<?php
$this->load->library('session');
$data = $this->session->all_userdata();
if(($this->uri->segment(2))!='search_keyword'){
    unset($data['browser']);
    unset($data['os']);
    unset($data['short_url']);
    unset($data['long_url']);
}
if (isset($data['os'])) {
    $os = $data['os'];
} else {
    $os = '';
}
if (isset($data['browser'])) {
    $browser = $data['browser'];
} else {
    $browser = '';
}
if (isset($data['short_url'])) {
    $short_url = $data['short_url'];
} else {
    $short_url = '';
}
if (isset($data['long_url'])) {
    $long_url = $data['long_url'];
} else {
    $long_url = '';
}
?>


<!-- Right side column. Contains the navbar and content of the page -->
<aside class="right-side">


    <section class="content-header">
        <h1>Reports OF URL Hits</h1>
        <ol class="breadcrumb">
            <li><a href="<?php base_url(); ?>dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="<?php base_url(); ?>searchurl">Search Url</a></li>
            <li class="active">URLs</li>
        </ol>
    </section>


    <!-- Content Header (Page header) -->
    <section class="content">

        <div class="box box-warning">
            <div class="box-header">
                <h3 class="box-title">Search By </h3> </div>
            <form role="form" method="post" action="<?php echo base_url(); ?>reports/search_keyword">
                <div class="box-body">
                    <div class="row">
                        <div class="col-xs-5">
                            <label for="">Long Url</label>
                            <input type="text" name="long_url" class="form-control" value="<?php echo $long_url;?>"   placeholder="long Url">
                        </div>	
                        <div class="col-xs-4">
                            <label for="url">Short Url</label>
                            <input type="text" class="form-control"  name="short_url" value="<?php echo $short_url;?>" placeholder="Short Url">
                        </div>
                        <div class="col-xs-5">
                            <label for="url">Start Date</label>
                            <input type="text" class="form-control" id="datepickerstart" name="create_timestart" placeholder="Start Date" value="<?php echo set_value('create_timestart'); ?>">
                        </div>
                        <div class="col-xs-4">
                            <label for="url">End Date</label>
                            <input type="text" class="form-control" id="datepickerend" name="create_timeend" placeholder="End Date" value="<?php echo set_value('create_timeend'); ?>">
                        </div>
                        <div class="col-xs-5">
                            <label for="url">Operating Syatem</label>
                            <select class="form-control" name="os">
                                <option selected value="">Select OS</option>
                                <?php foreach ($optionos as $data) { ?> 
                                    <option <?php if ($os == ($data->os)) {
                                    echo "selected";
                                } ?> value="<?php echo $data->os; ?>"><?php echo $data->os; ?></option> 
<?php } ?>
                            </select>
                        </div>
                        <div class="col-xs-4">
                            <label for="url">Browser</label>
                            <select class="form-control" name="browser">
                                <option selected value="">Select Browser</option>
                                <?php foreach ($optionbrowser as $data) { ?> 

                                    <option <?php if ($browser == ($data->browser)) {
                                        echo "selected";
                                    } ?> value="<?php echo $data->browser; ?>"><?php echo $data->browser; ?></option> 
<?php } ?>
                            </select>
                        </div>

                    </div>
                </div><!-- /.box-body -->
                <div class="box-footer">
                    <button type="submit" class="btn btn-primary" name="submit">
                        Search
                    </button>
                    &nbsp; &nbsp;
                    <a href="<?php echo base_url(); ?>reports" class="btn btn-primary"> Reset</a> &nbsp; &nbsp;

                </div>
            </form>    
        </div>



        <!-- Main content -->

        <div class="row">
            <?php
            $msg = $this->session->flashdata('msg');
            if ($msg != '') {
                echo "<div class=\"alert alert-success alert-dismissable\">  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button><strong>$msg</strong></div>";
            }
            ?>
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body table-responsive">


                        <table id="example2" class="table table-bordered table-hover">
                            <thead>
                                <tr>

                                    <th>Short Url</th>
                                    <th>Long Url</th>
                                    <th>Browser</th>
                                    <th>OS</th>
                                    <th>IP Address</th>
                                    <th>Date</th>

                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
<?php foreach ($results as $data) { ?> 
                                    <tr>

                                        <td><?php echo $data->short_url; ?></td>
                                        <td><div class="data" style="height:20px; width:350px; overflow:hidden;"><?php echo $data->long_url; ?></div></td>
                                        <td><?php echo $data->browser; ?></td>
                                        <td><?php echo $data->os; ?></td>
                                        <td class="btn btn-default btn-block"><span class="ip" ip="<?php echo base_url() ?>reports/ipdetail?id=<?php echo $data->ip; ?>"><?php echo $data->ip; ?></span></td>
                                        <td><?php echo date("d-M-Y h:i:sa ", strtotime($data->hit_time)); ?></td>

                                        <td>

                                            <a id="confirm" class="btn btn-danger btn-sm" rel="<?php echo $data->id; ?>" class="confirm" href="<?php echo base_url() ?>reports/DeleteUrl?id=<?php echo $data->id; ?>" style="color: white;" ><i class="fa fa-fw fa-trash-o"></i>Delete</a>
                                        </td>
                                    </tr>

                                    <?php } ?>

                            </tbody>
                        </table><div class="row"><div class="col-xs-6"><div class="dataTables_info" id="example2_info">
                                    <?php
                                    if ($this->pagination->total_rows == '0') {
                                        echo "<h3>No Record Found</h3>";
                                    } else {
                                        $param = $this->pagination->cur_page * $this->pagination->per_page;
                                        if ($param > $this->pagination->total_rows) {
                                            $param = $this->pagination->total_rows;
                                        }
                                        if ($this->pagination->cur_page == '0') {
                                            $param = $this->pagination->total_rows;
                                        }
                                        $off = $this->pagination->cur_page;
                                        if ($this->pagination->cur_page > '1') {
                                            $off = (($this->pagination->cur_page * '10') - 9);
                                        }
                                        echo "Showing " . $off . "-" . $param . " of " . $this->pagination->total_rows . " total results";
                                    }
                                    ?>
                                </div></div><div class="col-xs-6"><div class="pull-right">
                                    <ul class="pagination" style="margin: 5px 0;">
                                        <li><?php echo $links ?></li></ul></div></div></div></div>


                </div>
            </div><!-- /.box-body -->
        </div><!-- /.box -->

    </section><!-- /.content -->
</aside>
<!-- /.right-side -->
<script type="text/javascript">
    $("a#confirm").on('click', function(e) {

        e.preventDefault();
        var loc = $(this).attr('href');
        /*   alert($(this).attr('rel'))    ;
         return false; */

        bootbox.confirm("Are you sure, You want to delete the Url ?", function(confirmed) {
            if (confirmed) {
                location.href = loc;
            }
        });

    });

    $('.ip').on('click', function() {

        var url = $(this).attr('ip');
        $.ajax({
            type: "GET",
            url: url,
            dataType: "html",
            success: function(response) {
                bootbox.alert(response, function(confirmed) {
                    if (confirmed) {
                        location.href = loc;
                    }
                });
            }
        });

    });
</script> 
